package com.pucong.gitee.database.backup.business.unRelational.redis.service;

import com.pucong.gitee.database.backup.business.unRelational.redis.util.RedisStringUtil;
import com.pucong.gitee.database.backup.common.entity.redis.RedisConnect;
import com.pucong.gitee.database.backup.common.entity.redis.RedisInfo;
import com.pucong.gitee.database.backup.common.entity.redis.vo.RedisNetInfo;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.utils.RedisUtil;
import com.pucong.gitee.database.backup.utils.UserRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.util.Slowlog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class InfoSinglesServiceImpl implements InfoSinglesService {

    @Autowired
    private AutoRedisConnectService autoRedisConnectService;

    @Override
    public RedisInfo getBaseInfo(String id) {
        UserJwt loginUser = UserRequest.getLoginUser();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            return RedisUtil.getRedisInfoList(jedis);
        }
        return null;
    }

    @Override
    public List<Slowlog> getLogsInfo(String id) {
        UserJwt loginUser = UserRequest.getLoginUser();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            List<Slowlog> logs = RedisUtil.getRedisLog(jedis);
            Collections.reverse(logs);
            return logs;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public RedisNetInfo getMemInfo(String id) {
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        UserJwt loginUser = UserRequest.getLoginUser();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            String info = RedisUtil.getInfo(jedis, "memory");
            String[] memory = info.split("\n");
            String val01 = RedisStringUtil.getValueString(":", memory[1]).replace("\r", "");
            String val02 = RedisStringUtil.getValueString(":", memory[4]).replace("\r", "");
            redisNetInfo.setFull((float) (Math.round((Float.valueOf(val01) / 1048576) * 100)) / 100);
            redisNetInfo.setUsed((float) (Math.round((Float.valueOf(val02) / 1048576) * 100)) / 100);
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }
        return redisNetInfo;
    }

    @Override
    public RedisNetInfo getCpuInfo(String id) {
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        UserJwt loginUser = UserRequest.getLoginUser();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            String info = RedisUtil.getInfo(jedis, "cpu");
            String[] cpu = info.split("\n");
            String val01 = RedisStringUtil.getValueString(":", cpu[1]).replace("\r", "");
            String val02 = RedisStringUtil.getValueString(":", cpu[2]).replace("\r", "");
            redisNetInfo.setFull(Float.valueOf(val01));
            redisNetInfo.setUsed(Float.valueOf(val02));
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }

        return redisNetInfo;
    }

    @Override
    public Long[] getKeyInfo(String id) {
        Long[] keys;
        try {
            UserJwt loginUser = UserRequest.getLoginUser();
            RedisConnect connect = autoRedisConnectService.getRedisConnect(id);
            Jedis jedis = autoRedisConnectService.getJedis(id);
            if (null != connect && null != jedis) {
                String role = RedisUtil.getInfo(jedis, "server");
                boolean isCluster = role.contains("redis_mode:cluster");
                if (isCluster) {
                    keys = new Long[1];
                    keys[0] = RedisUtil.dbSize(jedis, null);
                } else {
                    int dbCount = RedisUtil.getDbCount(jedis);
                    keys = new Long[dbCount];
                    for (int i = 0; i < dbCount; i++) {
                        keys[i] = RedisUtil.dbSize(jedis, i);
                    }
                }
            } else {
                keys = new Long[16];
                for (int i = 0; i < 16; i++) {
                    keys[i] = 0L;
                }
            }
        } catch (Exception e) {
            keys = new Long[16];
            for (int i = 0; i < 16; i++) {
                keys[i] = 0L;
            }
        }
        return keys;
    }

    @Override
    public RedisNetInfo getNetInfo(String id) {
        RedisNetInfo redisNetInfo = new RedisNetInfo();
        UserJwt loginUser = UserRequest.getLoginUser();
        Jedis jedis = autoRedisConnectService.getJedis(id);
        if (null != jedis) {
            String info = RedisUtil.getInfo(jedis, "stats");
            String[] stats = info.split("\n");
            String val01 = RedisStringUtil.getValueString(":", stats[6]).replace("\r", "");
            String val02 = RedisStringUtil.getValueString(":", stats[7]).replace("\r", "");
            redisNetInfo.setFull( Float.valueOf(val01));
            redisNetInfo.setUsed( Float.valueOf(val02));
        } else {
            redisNetInfo.setFull(0F);
            redisNetInfo.setUsed(0F);
        }
        return redisNetInfo;
    }
}
